/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2020. All rights reserved.
 */

package com.huawei.chengdu.meetingdemo.view.dialogs;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.util.LogUtil;
import com.huawei.chengdu.meetingdemo.presenter.MeetingManager;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 类名：FloatWindowCustomBase
 * 描述：自定义悬浮窗抽象基类
 */
public abstract class FloatWindowCustomBase implements View.OnClickListener {
    private static final String TAG = "FloatWindowCustomBase";

    /**
     * 高亮OK
     */
    public static final Integer HIGH_LIGHT_OK = 1;

    /**
     * 高亮Cancel
     */
    public static final Integer HIGH_LIGHT_CAN = 2;

    /**
     * 窗口管理者
     */
    protected static WindowManager mWindowManager;

    /**
     * 是否显示浮动窗口
     */
    private static ConcurrentHashMap<String, FloatWindowCustomBase> windowMap = new ConcurrentHashMap<>();

    /**
     * 布局参数
     */
    protected WindowManager.LayoutParams wmParams;

    /**
     * 上下文
     */
    protected Context mContext;

    /**
     * view根布局
     */
    protected View mRootView;

    /**
     * 每个悬浮窗的id
     */
    protected String windowID;

    /**
     * 每一个悬浮窗都会关联一个CountDownTimer
     */
    private CountDownTimer countDownTimer;

    private OnBtnClickCallBack callBack;

    public FloatWindowCustomBase(Context context, String id) {
        mContext = context;
        windowID = id;
        floatWindowSetting();
        initRootView();
    }

    private void floatWindowSetting() {
        // 初始化窗口管理服务
        if (mWindowManager == null && (mContext.getSystemService(Context.WINDOW_SERVICE) instanceof WindowManager)) {
            mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        }

        // 初始化窗口布局参数
        if (wmParams == null) {
            wmParams = new WindowManager.LayoutParams();
            // 设置图片格式，效果为背景透明
            wmParams.format = PixelFormat.TRANSLUCENT;
            wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            wmParams.height = WindowManager.LayoutParams.MATCH_PARENT;

            wmParams.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

            wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }
    }

    /**
     * 获取跟布局id
     *
     * @return 布局id
     */
    protected abstract int getLayoutId();

    /**
     * 获取子类的名字
     *
     * @return 子类的名字
     */
    protected abstract String getChildName();

    private void initRootView() {
        mRootView = LayoutInflater.from(mContext).inflate(getLayoutId(), null);
    }

    /**
     * 窗口title
     *
     * @param tag 设置字段
     * @return 返回字段
     */
    protected String toWindowTitleString(String tag) {
        // 下面tag之前的前缀为设置title的固定字段，请误改动
        return "Window - " + "com.device.idea.meetingui" + "/" + tag;
    }

    /**
     * 设置windowmanager title参数
     *
     * @param title 设置值
     */
    protected void setWmTitleParams(String title) {
        String resultTitle = toWindowTitleString(title);
        LogUtil.debug(TAG + " resultTitle:" + resultTitle);
        LogUtil.info("handleParticipantsClick");
        wmParams.setTitle(resultTitle);
    }

    /**
     * setCallBack
     *
     * @param callBack callBack
     */
    public void setCallBack(CustomFloatWindow.OnBtnClickCallBack callBack) {
        this.callBack = callBack;
    }

    /**
     * 显示对话框,在指定时间后自动消失
     *
     * @param time 指定时间
     */
    public void showFloatWindow(int time) {
        showFloatWindow();
        countDownTimer = new CountDownTimer(time * 1000L, 1000) {
            @Override
            public void onTick(long arg0) {
            }

            @Override
            public void onFinish() {
                hideFloatWindow(windowID);
            }
        }.start();
    }

    /**
     * 显示对话框
     */
    public void showFloatWindow() {
        FloatWindowCustomBase oldWindow = windowMap.put(getWindowID(), this);
        if (oldWindow != null) {
            LogUtil.error(TAG + "already has a float window " + getWindowID());
            oldWindow.removeWindow();
        }
        if (MeetingManager.getInstance().isInConf()) {
            mWindowManager.addView(mRootView, wmParams);
        }
    }

    /**
     * 销毁弹窗
     */
    private void removeWindow() {
        mWindowManager.removeView(mRootView);
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    /**
     * 关闭会议的所有悬浮窗
     */
    public static void hideAllFloatWindow() {
        Iterator<Map.Entry<String, FloatWindowCustomBase>> windowIt = windowMap.entrySet().iterator();
        while (windowIt.hasNext()) {
            Map.Entry<String, FloatWindowCustomBase> windowEntry = windowIt.next();
            FloatWindowCustomBase windowCustomBase = windowEntry.getValue();
            if (windowCustomBase != null) {
                windowCustomBase.removeWindow();
                windowIt.remove();
            }
        }
    }

    /**
     * 根据悬浮窗 id来不显示悬浮窗,同时关闭倒计时器
     *
     * @param id 悬浮窗id
     */
    public static void hideFloatWindow(String id) {
        FloatWindowCustomBase windowCustomBase = windowMap.remove(id);
        if (windowCustomBase != null) {
            windowCustomBase.removeWindow();
        }
    }

    /**
     * getView
     *
     * @return View
     */
    public View getView() {
        return mRootView;
    }

    /**
     * getWindowID
     *
     * @return windowID
     */
    public String getWindowID() {
        return windowID;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.acceptAction) {
            callBack.isOkBtnClick();
        }
        if (id == R.id.rejectAction) {
            callBack.isCancelBtnClick();
        }
        if (id == R.id.close_dig_iv) {
            hideFloatWindow(windowID);
            LogUtil.debug(TAG + " click close");
        }
    }

    /**
     * 点击回呼
     */
    public interface OnBtnClickCallBack {
        /**
         * 确认，接听按钮点击
         */
        void isOkBtnClick();

        /**
         * 取消，拒接按钮点击
         */
        void isCancelBtnClick();
    }
}
